package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "Login";
    EditText mUser;
    EditText mPassword;
    Button mButton;
    private String user;
    private String password;
    CheckBox cb_Password,cb_Auto;
    private SharedPreferences sp1;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
       if (requestCode == 100){
           Log.d("xuesuhao","onRequestPermissionResult" + grantResults[0] + "," + grantResults[1]);
           if (grantResults[0] == PackageManager.PERMISSION_DENIED || grantResults[1] == PackageManager.PERMISSION_DENIED)
               finish();
       }
    }
    private void checkPermission(){
        int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,PERMISSIONS,100);
        }
    }

    private static String[] PERMISSIONS ={
           Manifest.permission.READ_EXTERNAL_STORAGE,
           Manifest.permission.WRITE_EXTERNAL_STORAGE,
           Manifest.permission.MOUNT_FORMAT_FILESYSTEMS,
           Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();

        mUser = findViewById(R.id.wrp_user);



        mPassword = findViewById(R.id.wrp_password);
        mButton = findViewById(R.id.btn_Login);
        cb_Password = findViewById(R.id.ck_pwd);
        cb_Auto = findViewById(R.id.ck_auto);

        sp1 = this.getSharedPreferences("1233",MODE_PRIVATE);
        final String username = sp1.getString("username","");//d得到账号
        final String password = sp1.getString("password","");//得到密码
        mUser.setText(username);
        mPassword.setText(password);


        final SharedPreferences sp = getSharedPreferences("Auser",MODE_PRIVATE);

        cb_Password.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                        Editor editor = sp.edit();
                        editor.putString("USER_NAME",user);
                        editor.putString("PASSWORD",password);
                        editor.commit();
                        Log.d("执行了","记住密码");
                        Toast.makeText(MainActivity.this,"记住密码已选中",Toast.LENGTH_SHORT).show();
                        sp.edit().putBoolean("ISCHECK",true).apply();
                }else {
                    Log.d("执行了","记住密码");
                    Toast.makeText(MainActivity.this,"记住密码已取消",Toast.LENGTH_SHORT).show();
                    sp.edit().putBoolean("ISCHECK",false).apply();
                }
            }
        });

        cb_Auto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this,"自动登录已选中",Toast.LENGTH_SHORT).show();
                    sp.edit().putBoolean("AUTO_ISCHECK",true).commit();
                }else {
                    Toast.makeText(MainActivity.this,"自动登录已取消",Toast.LENGTH_SHORT).show();
                    sp.edit().putBoolean("AUTO_ISCHECK",false).commit();
                }
            }
        });

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String Auser = mUser.getText().toString();
                final String Apassword = mPassword.getText().toString();

                final SharedPreferences sp = getSharedPreferences(Auser,MODE_PRIVATE);
                final String username = sp.getString("username",null);
                final String Password = sp.getString("password",null);


                if (sp.getBoolean("ISCHECK",false)){
                    cb_Password.setChecked(true);
                    mUser.setText(sp.getString("USER NAME",""));
                    mPassword.setText(sp.getString("PASSWORD",""));




                if(mUser.getText().toString().equals("") || mPassword.getText().toString().equals("") ){
                    Toast.makeText(MainActivity.this,"账号密码不能为空",Toast.LENGTH_SHORT).show();
                    }

                }else {new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            OkHttpClient client = new OkHttpClient();
                            Request request = new Request.Builder().url("http://10.0.2.2:8080/test/login.jsp?username="+Auser+"&password" + mPassword).build();
                            Response response = client.newCall(request).execute();
                            final String result = response.body().string();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(result.contains("1")){
                                        Toast.makeText(MainActivity.this,"登录成功！",Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(MainActivity.this,contents.class);
                                        startActivity(intent);
                                        finish();
                                    }else {
                                        Toast.makeText(MainActivity.this,"账号或密码错误！",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
                }

                if (Auser.equals(username)&&Apassword.equals(Password)){
                    Toast.makeText(MainActivity.this,"登录成功！",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this,contents.class);
                    startActivity(intent);
                    finish();
                }else {
                    Toast.makeText(MainActivity.this,"账号或密码错误！",Toast.LENGTH_SHORT).show();
                }


            }
        });


    }

    public void zhuce_dp(View view) {
        Intent intent = new Intent(MainActivity.this,Zhuce.class);
        startActivity(intent);
    }

}
